package com.epapyrus.plugpdf;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;


import com.epapyrus.plugpdf.core.annotation.tool.AnnotSetting;
import com.epapyrus.plugpdf.core.annotation.tool.BaseAnnotTool;

public class AnnotToolSelectMenu {
    private Context mContext;
    private LayoutInflater mInflater;
    private PopupWindow mPopupWindow;

    private Button mEditNoteButton;
    private Button mEditInkButton;
    private Button mEditCircleButton;
    private Button mEditSquareButton;
    private Button mEditLineButton;
    private Button mEditHighlightButton;
    private Button mEditUnderlineButton;
    private Button mEditStrikeoutButton;

    private static final String PX = "px";
    private static final String PERCENT = "%";

    private OnAnnotToolSelectedListener listener;

    /**
     * Constructor that assigns the context and initializes the {@link LayoutInflater}.
     *
     * @param context {@link Context} instance
     */
    public AnnotToolSelectMenu(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * it creates the annotation tool slect pop-up window.
     *
     */
    @SuppressLint("InflateParams")
    private void createPopupWindow() {
        final AnnotSetting setting = AnnotSetting.instance();

        View menuView = mInflater.inflate(R.layout.annot_select, null);

        mPopupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        mEditNoteButton = (Button) menuView.findViewById(R.id.note_tool);
        mEditInkButton = (Button) menuView.findViewById(R.id.ink_tool);
        mEditCircleButton = (Button) menuView.findViewById(R.id.circle_tool);
        mEditSquareButton = (Button) menuView.findViewById(R.id.square_tool);
        mEditLineButton = (Button) menuView.findViewById(R.id.line_tool);
        mEditHighlightButton = (Button) menuView.findViewById(R.id.highlight_tool);
        mEditUnderlineButton = (Button) menuView.findViewById(R.id.underline_tool);
        mEditStrikeoutButton = (Button) menuView.findViewById(R.id.strikeout_tool);

        mEditNoteButton.setOnClickListener(mEditButtonClickHandler);
        mEditInkButton.setOnClickListener(mEditButtonClickHandler);
        mEditCircleButton.setOnClickListener(mEditButtonClickHandler);
        mEditSquareButton.setOnClickListener(mEditButtonClickHandler);
        mEditLineButton.setOnClickListener(mEditButtonClickHandler);
        mEditHighlightButton.setOnClickListener(mEditButtonClickHandler);
        mEditUnderlineButton.setOnClickListener(mEditButtonClickHandler);
        mEditStrikeoutButton.setOnClickListener(mEditButtonClickHandler);
    }

    /**
     * Given both the annotation type and the position of the annotation pop-up, it creates and shows
     * the annotation tool's pop-up.
     *
     * @param anchor  A parent {@link View} to get Window token from
     * @param x       The popup's x location offset
     * @param y       The popup's y location offset
     */
    public synchronized void show(final View anchor, final int x, final int y) {

        createPopupWindow();

        mPopupWindow.showAtLocation(anchor, Gravity.CENTER, x, y);
    }

    /**
     * Closes the annotation pop up.
     */
    public synchronized void close() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    public PopupWindow getPopupWindow(){
        return mPopupWindow;
    }

    public void setAnnotToolSelectedListener(OnAnnotToolSelectedListener toolSelectedListener){
        listener = toolSelectedListener;
    }

    EditButtonClickHandler mEditButtonClickHandler = new EditButtonClickHandler();

    private class EditButtonClickHandler implements View.OnClickListener{

        private boolean mLongClickEvented = false;

        @Override
        public void onClick(View v) {
            if (mLongClickEvented) {
                mLongClickEvented = false;
                return;
            }

            if (v.getId() == mEditInkButton.getId()) {

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.INK);
                close();

            } else if(v.getId() == mEditCircleButton.getId()){

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.CIRCLE);
                close();

            } else if(v.getId() == mEditSquareButton.getId()){

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.SQUARE);
                close();

            } else if(v.getId() == mEditLineButton.getId()){

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.LINE);
                close();

            } else if (v.getId() == mEditHighlightButton.getId()) {

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.HIGHLIGHT);
                close();

            } else if (v.getId() == mEditUnderlineButton.getId()) {

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.UNDERLINE);
                close();

            } else if (v.getId() == mEditStrikeoutButton.getId()) {

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.STRIKEOUT);
                close();

            } else if (v.getId() == mEditNoteButton.getId()) {

                listener.ToolSelected(BaseAnnotTool.AnnotToolType.NOTE);
                close();

            }
        }
    }
}
